package com.iptv.grape.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.iptv.grape.RunTimeApplication;

import java.util.Map;

public class SharedUtil {
    public final static String FILE_DEFUALT ="defFile";
    public final static String USER ="User";

    public static void put(String fileName,String key,Object value ){
        SharedPreferences.Editor editor=  getEditor(fileName);
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else {
            editor.putString(key, value.toString());
        }
        editor.apply();
    }

    public static SharedPreferences getSp(String fileName){
        return RunTimeApplication.getCurApp().getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }
    public static SharedPreferences.Editor getEditor(String fileName){
        return getSp(fileName).edit();
    }

    /**
     * 移除某个key值已经对应的值
     *
     * @param key
     */
    public static void remove(String fileName,String key) {
        SharedPreferences.Editor editor =getEditor(fileName);
        editor.remove(key);
        editor.apply();
    }
    /**
     * 清除所有的数据
     */
    public static void clear(String fileName) {
        getEditor(fileName).clear();
    }

    /**
     * 查询某个key是否存在
     *
     * @param key
     * @return
     */
    public static boolean contains(String fileName,String key) {
        return getSp(fileName).contains(key);
    }

    /**
     * 返回所有的键值对
     *
     * @return
     */
    public static Map<String, ?> getAll(String fileName) {
        return getSp(fileName).getAll();
    }


}
